
<script type="text/javascript" src="../media/system/js/jQuery.vietvang.validate.js"></script>
  <script type="text/javascript">
    jq(document).ready(function(){
      jq('#adminForm').validate({
      });
    });
</script>
<form id="adminForm" action="index.php?option=com_mail_group" method="post" enctype="multipart/form-data" name="adminForm" class="form-validate">
   <input type="hidden" name="old_code" value="<?php echo $this->update_group->MAGR_CODE;?>"/>
   <fieldset class="adminform">
      <table class="adminformlist" style="width: 425px">
         <tr <?php if(empty($this->update_group->MAGR_CODE)) echo "style='visibility: hidden'"?>>
            
            <td><label style="text-align: right;font-size: 1.091em;padding-right:5px;">ID</label></td>
            <td><label class="label_questionare_id"><?php echo $this->update_group->MAGR_CODE;?></label></td>

         </tr>
         <tr>
            <td><label id="jform_state-lbl" for="jform_state" class="hasTip" title="" style="text-align: right;font-size: 1.091em;padding-right:5px;" >グループ名<span style="color: red;font-size: small;font-family: 'ＭＳ Ｐゴシック', Osaka;">※</span></label>     </td>
            <td>
              <input vvName="fullname" data_required="true" id="g_name" name="g_name" class="inputbox big" value="<?php echo $this->update_group->MAGR_NAME ?>" />
              <span name="msg_error"></span>
            </td>
            
         </tr>
         <tr>
            <td><label style="text-align: right;font-size: 1.091em;padding-right:5px;">ユーザタイプ</label></td>				
            <td><select id="type" name="type" class="inputbox medium">               
               <option value="0" <?php if ($this->update_group->MAGR_USERTYPE == 0) echo 'selected'?>>本部ユーザ</option>               
               <option value="3" <?php if ($this->update_group->MAGR_USERTYPE == 3) echo 'selected'?>>センターユーザ</option>
               <option value="4" <?php if ($this->update_group->MAGR_USERTYPE == 4) echo 'selected'?>>クラブユーザ</option>
               <option value="1" <?php if ($this->update_group->MAGR_USERTYPE == 1) echo 'selected'?>>指導員</option>
               <option value="2" <?php if ($this->update_group->MAGR_USERTYPE == 2) echo 'selected'?>>外部ユーザ</option>
            </select></td>
         </tr>
        </table>
   </fieldset>
   
   <div id='external' class="h_form">
      <p>外部ユーザ</p>
      <p><label> メールアドレス登録&nbsp;&nbsp;&nbsp;</label><input name="filecsv" type="file" accept=".csv"/></p>
    </div>
   <div id="relationship" class="h_form">
     
      <table>
        <tr>    
            <td style="text-align: right;font-size: 1.091em;padding-right:5px;">B&G関係ユーザ</td>
        </tr>
        <tr>
            <td style="width: 150px;"></td>
            <td style="text-align: right;font-size: 1.091em;padding-right:5px;">地区</td>
            <td>
                <select id="area" class="inputbox medium">
                   <option value='all'>全て</option>
                   <?php 
                        foreach($this->getModel()->getArea() as $area){
                            echo "<option value='$area->AREA_CODE'>$area->AREA_NAME</option>";
                        }
                   ?>
                </select>
            </td>
            <td style="text-align: right;font-size: 1.091em;padding-right:5px;">都道府県</td>
            <td>
                <select id="pref" class="inputbox medium">
                   <option value='all'>全て</option>
                   <?php 
                        foreach($this->getModel()->getPrefs() as $pref){
                            echo "<option value='$pref->PREF_CODE'>$pref->PREF_NAME</option>";
                        }
                   ?>
                </select>
            </td>
            <td style="text-align: right;font-size: 1.091em;padding-right:5px;">センタークラブ</td>
            <td>
                <input id="keyword" type="text" class="inputbox medium"/>
            </td>
        </tr>
      </table><br />
      <p><input id="search" type="button" value="検索" style="width: 60px;"/></p>
      <table class="adminlist" id="result">
		<thead>
			<tr>
        <?php if($this->update_group->MAGR_USERTYPE != 1){?>
        <th width="1%">
					<input id="checkall" type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
				</th>
				<th class="left">ユーザ名 </th>
				<th class="left" style="width: 60%">メールアドレス</th>
			</tr>
      <?php } else { ?>
        <tr>
          <th  width='1%'>
            <input type='checkbox' name='checkall-toggle' value='' title='" . JText::_('JGLOBAL_CHECK_ALL') . "' onclick='Joomla.checkAll(this)' />
          </th>
          <th class='left'>ユーザ名 </th>
          <th class='left'>自宅メールアドレス</th>
          <th class='left'>携帯メールアドレス</th>
          <th class='left'>勤務先メールアドレス</th>
        </tr>
      <?php } ?>

		</thead>
	
		<tbody >
        <?php
        if($this->update_group->MAGR_USERTYPE != 2){
             $details = $this->details;
            if(!empty($details)){
                $in_list = array();
                foreach($details as $val){
                    array_push($in_list, $val->id);
                }
                foreach ($this->total_group as $key => $row) {
                    $row_cls = $key % 2;
                    $mail = $row->mail_flag == '1' ? $row->email : '拒む';
                    
                    if(in_array($row->id, $in_list) ){
                        $checked = "checked = \"checked\"";
                    } else {
                      $checked = "";
                    }
                    echo "<tr class='row$row_cls'>
            			<td class='left'><input type='checkbox' id='cb0' name='cid[]' $checked value='$row->id'/></td>
            			<td style='width: 150px'>$row->name</td>
            			<td class='left' style='width: 300px'>$mail</td>";
            		  
                  if($this->update_group->MAGR_USERTYPE == 1){
                    $m_mail = $row->m_mail_flag == '1' ? $row->m_email : '拒む';
                    $w_mail = $row->w_mail_flag == '1' ? $row->w_email : '拒む';
                    echo 
                    " <td class='left' style='width: 300px'>$m_mail</td>
                      <td class='left' style='width: 300px'>$w_mail</td>";
                  }

                  echo "</tr> "; 
                }  
            }
        }     
        
        ?>
		</tbody>
	</table>
   

    <br /> <br />
   </div>
    <?php
    if($this->update_group->MAGR_USERTYPE != 1){
          echo '<div>
              <p style="font-size: 1.091em;">メールグループのメールアドレス一覧</p>
            <table class="adminlist">
          <thead>
            <tr>
              <th class="left">ユーザ名 　</th>
              <th class="left" style="width: 60%">メールアドレス</th>
            </tr>
          </thead>
        
          <tbody>';
          $details = $this->details;
          if(!empty($details))
              foreach ($this->details as $key => $row){
              $row_cls = $key % 2;
              echo "<tr class='row$row_cls'>
                <td class='left'>$row->name</td>
                <td class='left'>$row->email</td>
                </tr>";
                      
              }
          echo "</tbody>
        </table>
         </div>"; 
       } else {
            echo '<div>
                <p style="font-size: 1.091em;">メールグループのメールアドレス一覧</p>
              <table class="adminlist">
            <thead>
              <tr>
                <th class="left">ユーザ名 　</th>
                <th class="left">自宅メールアドレス</th>
                <th class="left">携帯メールアドレス</th>
                <th class="left">勤務先メールアドレス</th>
              </tr>
            </thead>
          
            <tbody>';
            $details = $this->details;
            if(!empty($details))
                foreach ($details as $key => $row){
                  $mail = $row->mail_flag == '1' ? $row->email : '拒む';
                  $m_mail = $row->m_mail_flag == '1' ? $row->m_email : '拒む';
                  $w_mail = $row->w_mail_flag == '1' ? $row->w_email : '拒む';
                $row_cls = $key % 2;
                echo "<tr class='row$row_cls'>
                  <td class='left'>$row->name</td>
                  <td class='left' style='width: 300px'>$mail</td>
                  <td class='left' style='width: 300px'>$m_mail</td>
                  <td class='left' style='width: 300px'>$w_mail</td>
                  </tr>";
                        
                }
            echo "</tbody>
          </table>
           </div>"; 
       }
    
    ?>
   
   <input type="hidden" name="task" value=""/>
   <input type="hidden" name="action" value="update" />
</form>
<script>
    Joomla.submitbutton = function(task){
		if (task == 'cancel' ){
      window.location.href = "index.php?option=com_mail_group";
    }else {
			Joomla.submitform(task, document.getElementById('adminForm'));
		}
	}
    
    jq('#area').change(function(event){
        jq('#pref').load('index.php?option=com_mail_group&task=ajax_get_pref&tmpl=component&area_code='+jq('#area').val());
        //alert (jq('#pref').val());'
    });
    
    jq('#type').change(function(event){
        jq('.h_form').hide();
        
        if (jq('#type').val() == 2){
            jq('#external').show();
        } else {
            jq('#relationship').show();
        }
    });
    
    jq(function(){
        jq('.h_form').hide();
        
        if (jq('#type').val() == 2){
            jq('#external').show();
        } else {
            jq('#relationship').show();
        }
        
        //jq("#checkall").attr("checked",true);
        //Joomla.checkAll(document.getElementById('checkall'));
        //jq("#checkall").attr("checked",true);
    });
    
    jq('#search').click(function(){
        jq.ajax({
          url: 'index.php?option=com_mail_group&task=ajax_search&tmpl=component'
            +'&keyword='+encodeURI(jq('#keyword').val())
            +'&type='+jq('#type').val()
            +'&area='+jq('#area').val()
            +'&pref='+jq('#pref').val(),
          context: document.body
        }).done(function(data ) {
          jq('#result').html(data);
        });
        jq("#checkall").attr("checked",false);
    });
    
    // jq('#type').change(function(even){
    //      jq.ajax({
    //       url: 'index.php?option=com_mail_group&task=ajax_search&tmpl=component'
    //         +'&keyword='+jq('#keyword').val()
    //         +'&type='+jq('#type').val()
    //         +'&area='+jq('#area').val()
    //         +'&pref='+jq('#pref').val(),
    //       context: document.body
    //     }).done(function(data ) {
    //       jq('#result').html(data);
    //     });
    //     jq("#checkall").attr("checked",false);
    // });   
</script>